﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using BookSystem.CommomHelper;
using BookSystem.Model;

namespace BookSystem.User
{
    public partial class UserGetBook : Form
    {
        public UserGetBook()
        {
            InitializeComponent();
        }
        SqlConnection conn = SQLHelper.getConnnection();
        SqlCommand cmd;

        string sql = "select * from T_books";


        private void dataBinding(String strSql) {

            SqlDataAdapter da;
            DataSet ds;


            //连接配置

           
            cmd = new SqlCommand(strSql, conn);
            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds, "myTable");

            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = "myTable";
            da.Dispose();

        
        
        }

        private void UserGetUser_Load(object sender, EventArgs e)
        {
            dataBinding(sql);
            dataGridView1.ReadOnly = true;

        }

        //dataGridView鼠标选中事件
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
            textBox2.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
            textBox3.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
            textBox4.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();

        }


        //查询按钮
        private void get_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == string.Empty)
            {

                MessageBox.Show("请输入关键字图书编号", "提示");
                textBox1.Focus();
                textBox1.SelectAll();
                return;

            }


            string sSQL = "select * from T_books where Bid = " + textBox1.Text;

            //刷新DataGridView控件
            dataBinding(sSQL);
        }

        //借阅按钮事件
        private void borrow_Button_Click(object sender, EventArgs e)
        {
            string date = DateTime.Now.ToShortDateString();

            if (BLL.Borrow.addBorrow(textBox1.Text, Main.id, date))
            {
                MessageBox.Show("借阅成功!");
                textBox1.Text = textBox2.Text = textBox3.Text = textBox4.Text = "";
                borrow_Button.Enabled = false;
            }
            else {

                MessageBox.Show("借阅失败，请重试!");
            
            }
            

        }

        //图书编号发生改变时候的事件
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            borrow_Button.Enabled = !(BLL.Borrow.isBorrowed(textBox1.Text));
        }


    

        private void UserGetBook_FormClosed(object sender, FormClosedEventArgs e)
        {
            cmd.Dispose();
            conn.Close();
        }






    }
}
